<template>
	<view class="container">
		<fu-navbar title="备件入库" bgColor="#0f68f0" color="#fff" isBackText></fu-navbar>
		<view class="bg-f padding-lr-24" style="border-radius: 0 0 20rpx 20rpx;">
			<view class="flex align-center padding-tb-32 border-b-c9">
				<image src="../../../static/images/icon31.png" mode="widthFix" class="i-icon margin-r-24"></image>
				<text class="fs-32 text-bold">基本信息</text>
			</view>
			<!-- <view class="flex align-center padding-tb-32 border-b-e4 justify-between fs-28">
				<view>入库仓库</view>
				<view class="flex align-center" @click="toggleTab('linkage')">
					<text v-if="linkValue">{{linkValue}}</text>
					<image src="../../../static/images/arrow-r.png" style="width: 32rpx;height: 32rpx; margin-left: 8rpx;"></image>
				</view>
			</view> -->
			<view class="flex align-center padding-tb-32 border-b-e4 justify-between fs-28">
				<view>入库类型</view>
				<view class="flex align-center" @click="toggleTab('selector')">
					<text class="text-ellipsis" v-if="typeValue">{{typeValue}}</text>
					<image src="../../../static/images/arrow-r.png" style="width: 32rpx;height: 32rpx; margin-left: 8rpx;"></image>
				</view>
			</view>
			<!-- <view class="flex align-center padding-tb-32 border-b-e4 justify-between fs-28">
				<view>关联单据</view>
				<view class="flex align-center" @click="navTo(`/pages/spare-parts/work-order/index`)">
					<view class="text-ellipsis text-underline" style="color: #02A7F0;">
						离心式空压机-240324002-0004
					</view>
					<image src="../../../static/images/arrow-r.png" style="width: 32rpx;height: 32rpx; margin-left: 8rpx;"></image>
				</view>
			</view> -->
		</view>
		<view class="margin-t-32 bg-f padding-lr-24" style="border-radius: 20rpx 20rpx 0 0;">
			<view class="flex align-center padding-tb-32 border-b-c9 justify-between">
				<view class="flex align-center">
					<image src="../../../static/images/icon32.png" mode="widthFix" class="margin-r-24" style="width: 60rpx;"></image>
					<text class="fs-32 text-bold">备件列表</text>
				</view>
				<view>
					<image src="../../../static/images/icon26.png" style="width: 52rpx;height: 52rpx;" @click="scanClick"></image>
				</view>
			</view>
			<view class="padding-tb-12">
				<del-slide-left v-for="(item, index) in partList" :key="index" :item="item"  :data_transit="{index:index,item:item}" @delItem="cancel(index)">
					<view class="item padding-tb-12">
						<view class="flex align-center justify-between over-hidden">
							<view class="fs-30 text-bold text-ellipsis">{{item.name}}</view>
							<!-- <view class="fs-30">库位：12</view> -->
							<!-- <image src="../../../static/images/close2.png" style="width: 32rpx;height: 32rpx;margin-left: 32rpx;" @click="cancel(index)"></image> -->
						</view>
						<view class="flex over-hidden margin-t-24">
							<image src="../../../static/images/no-img.png" class="img flex-shrink" mode="aspectFill"></image>
							<view class="flex-1 over-hidden">
								<view class="fs-26 flex">
									<view class="label">编码：</view>
									<view class="text-ellipsis">{{item.code}}</view>
								</view>
								<view class="fs-26 flex">
									<view class="label">分类：</view>
									<view class="text-ellipsis">{{item.brand}}</view>
								</view>
								<view class="fs-26 flex">
									<view class="label">规格型号：</view>
									<view class="text-ellipsis">{{item.specs}}</view>
								</view>
								<view class="fs-26 flex">
									<view class="label">仓库：</view>
									<view class="text-ellipsis">{{item.warehouse || ''}}</view>
								</view>
								<view class="fs-26 flex">
									<view class="label">库位：</view>
									<view class="text-ellipsis">{{item.locator || ''}}</view>
								</view>
							</view>
							<view class="flex-shrink flex align-end margin-b-12">
								<view class="flex align-center">
									<image src="../../../static/images/jian2.png" class="icon" @click="changeNum(item, index, 'sub')"></image>
									<view class="num">{{item.num}}</view>
									<image src="../../../static/images/jia2.png" class="icon" @click="changeNum(item, index, 'add')"></image>
								</view>
							</view>
						</view>
					</view>
				</del-slide-left>
			</view>
			<view class="flex justify-center align-center" style="height: 200rpx;" v-if="partList.length == 0">
				<image src="../../../static/empty/no-data.png" mode="widthFix" style="width: 160rpx;"></image>
			</view>
		</view>
		<view class="bottom-wrap">
			<view class="bottom flex align-center justify-between fs-36">
				<!-- <view class="btn" >保存</view> -->
				<view class="btn" @click="insertPart">入库</view>
			</view>
		</view>
		
		<!-- 入库仓库 -->
		<w-picker mode="linkage" :options="linkList" themeColor="#30a3e0" :defaultProps="linkProps" default-type="name" @confirm="onConfirm($event, 'linkage')" ref="linkage"></w-picker>
		<!-- 入库类型 -->
		<w-picker mode="selector" :options="typeList" themeColor="#30a3e0" :defaultProps="defaultProps" default-type="dictName" @confirm="onConfirm($event, 'selector')" ref="selector"></w-picker>
	</view>
</template>

<script>
	import delSlideLeft from '@/components/delSlideLeft/delSlideLeft.vue';
export default {
	components: {
		delSlideLeft
	},
	data() {
		return {
			typeList: [],
			linkProps: {
				value: 'id',
				label: 'name',
				children: 'children'
			},
			defaultProps: {
				value: 'dictValue',
				label: 'dictName'
			},
			typeValue: '',
			inType: null,
			partList: [],
			linkList: [],
			linkValue: null,
			scanPart: {}
		}
	},
	onLoad() {
		this.getPartLink();
		this.getInType();
		uni.$on('scanTap', (id, stockId) => {
			this.getPart(id, stockId)
		})
	},
	onUnload() {
		uni.$off('scanTap')
	},
	methods: {
		cancel(i) {
			this.partList.splice(i, 1)
		},
		navTo(path) {
			uni.navigateTo({
				url:path
			})
		},
		toggleTab(ref) {
			this.$refs[ref].show();
		},
		onConfirm(e, type) {
			console.log(e)
			if (type == 'selector') {
				this.typeValue = e.result
				this.inType = e.value
			} else if (type == 'linkage') {
				this.linkValue = e.result
			}
		},
		// 查询入库类型
		getInType() {
			this.$api.post(global.apiUrls.selectType, {
				dictTypeCode: 'TYPE_PART_IN_TYPE'
			}).then(res => {
				console.log(res)
				if (res.data.code == 9100) {
					this.typeList = res.data.info
				}
			})
		},
		changeNum(e, i, type) {
			if (type == 'sub') {
				if (e.num == 1) return
				this.partList[i].num--
			} else {
				if (e.num == e.stock) {
					uni.showToast({
						title: '暂无库存',
						icon: 'none'
					})
				} else {
					this.partList[i].num++
				}
			}
			this.$forceUpdate();
		},
		scanClick() {
			// #ifdef APP-PLUS
			// uni.scanCode({
			// 	success: res => {
			// 		console.log(res, 'scan')
			// 		if (res.result.startsWith('EAM_STOCK')) {
			// 			let id = res.result.split('_')[2]
			// 			let stockId = res.result.split('_')[3]
			// 			this.getPart(id, stockId)
			// 		}
			// 	}
			// })
			// #endif
			uni.navigateTo({
				url: `/pages/service/scanCode/index?type=part`
			})
		},
		// 查询备件
		getPart(id, stockId) {
			let that = this
			this.$api.post(global.apiUrls.selectPartInfo, {
				id: id
			}).then(res => {
				if (res.data.code == 9100) {
					console.log(res.data)
					let data = res.data.info
					data.num = 1
					data.stockId = stockId
					that.scanPart = data
					// that.partList.push(data)
					that.getStock(stockId);
				} else {
					uni.showToast({
						title: res.data.msg,
						icon: 'none'
					})
				}
			})
		},
		// 查询库存信息
		getStock(id) {
			let that = this
			this.$api.post(global.apiUrls.selectStockInfo, {
				id: id
			}).then(res => {
				if (res.data.code == 9100) {
					let data = res.data.info
					that.scanPart.warehouse = data.warehouse
					that.scanPart.locator = data.locator
					that.partList.push(that.scanPart)
				} else {
					uni.showToast({
						title: res.data.msg,
						icon: 'none'
					})
				}
			})
		},
		// 查询备件仓库
		getPartLink() {
			this.$api.post(global.apiUrls.selectPartWarehouseLink).then(res => {
				if (res.data.code == 9100) {
					this.linkList = res.data.info
				}
			})
		},
		// 入库
		insertPart() {
			if (!this.inType) {
				uni.showToast({
					title: '请选择入库类型',
					icon: 'none'
				})
				return
			}
			let arr = []
			this.partList.forEach(item => {
				let obj = {
					partId: item.id,
					stockId: item.stockId,
					num: item.num
				}
				arr.push(obj)
			})
			this.$api.post(global.apiUrls.insertPartIn, {
				inType: this.inType,
				data: JSON.stringify(arr)
			}).then(res => {
				if (res.data.code == 9100) {
					uni.showToast({
						title: '操作成功',
						icon: 'none'
					})
					setTimeout(() => {
						uni.navigateBack()
					}, 800)
				} else {
					uni.showToast({
						title: res.data.msg,
						icon: 'none'
					})
				}
			})
		}
	}
}
</script>

<style lang="scss" scoped>
.container {
	min-height: 100vh;
	background-color: #f3f5f8;
}
.i-icon {
	width: 40rpx;
}
.item {
	.label {
		width: 136rpx;
		text-align: right;
		flex-shrink: 0;
	}
	.img {
		width: 100rpx;
		height: 100rpx;
		margin-right: 12rpx;
	}
	.icon {
		width: 32rpx;
		height: 32rpx;
	}
	.num {
		width: 70rpx;
		border: 1rpx solid #b4b4b4;
		margin: 0 12rpx;
		font-size: 26rpx;
		text-align: center;
	}
}
.bottom-wrap {
	height: 140rpx;
	.bottom {
		padding: 16rpx 48rpx;
		position: fixed;
		bottom: 0;
		width: 100%;
		box-sizing: border-box;
		z-index: 9;
		.btn {
			width: 100%;
			height: 98rpx;
			border-radius: 12rpx;
			text-align: center;
			line-height: 98rpx;
			color: #fff;
			background-color: #0f68f0;
		}
	}
}
</style>